package cn.jietuo.first.class07;

import java.util.LinkedList;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/7/20
 * @description: 按层遍历二叉树，即宽度优先
 */
public class Code03_LevelTraversalBT {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    public static void levelPrint(Node head){
        if (head == null){
            return;
        }
        LinkedList<Node> list = new LinkedList<>();
        list.add(head);
        while (!list.isEmpty()){
            Node poll = list.poll();
            System.out.println(poll.value);
            if (poll.left != null){
                list.add(poll.left);
            }
            if (poll.right != null){
                list.add(poll.right);
            }
        }
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        levelPrint(head);
        System.out.println("========");
    }



}
